//
//  Math.h
//  leetcode
//
//  Created by hellobike07499 on 2019/9/4.
//  Copyright © 2019 hellobike07499. All rights reserved.
//

#ifndef Math_h
#define Math_h
#include <iostream>
#include <stack>
#include <vector>

using namespace std;
class CMath{
public:
    void test(){
        test3();
    }
    
    void test0(){
        vector<string> n = fizzBuzz(30);
        for (int i = 0; i < n.size(); i++) {
            cout << n[i] << ",";
        }
    }
    
    void test1(){
        cout << countPrimes(10) << endl;
        cout << countPrimes(499979) << endl;
    }
    
    void test2(){
        cout << (isPowerOfThree(1) ? "true" : "false") << endl;
        cout << (isPowerOfThree(10) ? "true" : "false") << endl;
    }
    void test3(){
        cout << romanToInt("III") << endl;
        cout << romanToInt("IV") << endl;
        cout << romanToInt("IX") << endl;
        cout << romanToInt("LVIII") << endl;
        cout << romanToInt("MCMXCIV") << endl;
    }
    
private:
    vector<string> fizzBuzz(int n) {
        vector<string> result(n, "");
        for (int i = 0; i < n; i++) {
            result[i] = to_string(i + 1);
        }
        for (int i = 1; i * 3 <= n; i++) {
            result[i * 3 - 1] = "Fizz";
        }
        
        for (int i = 1; i * 5 <= n; i++) {
            if(i % 3 == 0){
                result[i * 5 - 1] = "FizzBuzz";
            }else{
                result[i * 5 - 1] = "Buzz";
            }
        }
        return result;
    }
    
    int countPrimes(int n) {
        if(n <= 1) return 0;
        
        int count = 0;
        vector<bool> info(n, true);
        for (int i = 2; i < n; i++) {
            if(!info[i]) continue;
            
            count++;
            for (unsigned long j = i; j * i < n; j++) {
                info[j * i] = false;
            }
        }
        return count;
    }
    
    bool isPowerOfThree(int n) {
        if(n < 1) return false;
        if(n == 1) return true;
        
        return (n % 3 == 0 && isPowerOfThree(n / 3));
    }
    
    int romanToInt(string s) {
        if(s.length() == 0) return 0;
        
        int all = 0;
        int move = 0;
        
        int lastValue = 0;
        
        while(move < s.length()){
            char c = s[move];
            if(c == 'I'){
                all += lastValue;
                
                lastValue = 1;
            }else if(c == 'V'){
                if(lastValue == 1){
                    lastValue = 4;
                }else{
                    all += lastValue;
                    lastValue = 5;
                }
                
            }else if(c == 'X'){
                if(lastValue == 1) lastValue = 9;
                else {
                    all += lastValue;
                    lastValue = 10;
                }
            }else if(c == 'L'){
                if(lastValue == 10){
                    lastValue = 40;
                }else{
                    all += lastValue;
                    lastValue = 50;
                }
                
            }else if(c == 'C'){
                if(lastValue == 10){
                    lastValue = 90;
                }else{
                    all += lastValue;
                    lastValue = 100;
                }
                
            }else if(c == 'D'){
                if(lastValue == 100){
                    lastValue = 400;
                }else{
                    all += lastValue;
                    lastValue = 500;
                }
            }else if(c == 'M'){
                if(lastValue == 100){
                    lastValue = 900;
                }else{
                    all += lastValue;
                    lastValue = 1000;
                }
            }
            move++;
        }
        
        all += lastValue;
        
        return all;
    }
};

#endif /* Math_h */
